package com.pet.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pet.model.media.pojos.WmFolders;
import com.pet.model.media.pojos.WmUser;
import com.pet.model.media.pojos.WmUserFriend;
import com.pet.utils.threadlocal.WmThreadLocalUtils;
import com.pet.wemedia.mapper.WmFoldersMapper;
import com.pet.wemedia.mapper.WmUserFriendMapper;
import com.pet.wemedia.service.WmFoldersService;
import com.pet.wemedia.service.WmUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class WmFoldersServiceImpl implements WmFoldersService {

    @Resource
    private WmFoldersMapper wmFoldersMapper;
    @Autowired
    WmUserFriendMapper wmUserFriendMapper;
    @Autowired
    private WmUserService userService;

    @Override
    public boolean save(WmFolders folder) {
        // 使用MyBatis-Plus的insert方法保存文件夹
        WmUser user = userService.getById(folder.getUserId());
        folder.setCreatedBy(user.getName());
        int result = wmFoldersMapper.insert(folder);
        return result > 0;
    }

    @Override
    public boolean removeById(Integer id) {
        // 使用MyBatis-Plus的deleteById方法删除文件夹
        int result = wmFoldersMapper.deleteById(id);
        return result > 0;
    }

    @Override
    public WmFolders getById(Integer id) {
        // 使用MyBatis-Plus的selectById方法获取文件夹
        return wmFoldersMapper.selectById(id);
    }

    @Override
    public boolean updateById(WmFolders folder) {
        // 使用MyBatis-Plus的updateById方法更新文件夹
        int result = wmFoldersMapper.updateById(folder);
        return result > 0;
    }

    @Override
    public List<WmFolders> list(Integer userId) {
        List<WmFolders> list = new ArrayList<>();
        Integer uid = WmThreadLocalUtils.getUser().getId();
        List<WmFolders> meList = wmFoldersMapper.selectList(new QueryWrapper<WmFolders>().eq("user_id", uid));
        if(userId!=null){
            return meList;
        }
        // 使用MyBatis-Plus的selectList方法查询文件夹列表
        //查询文件夹状态为全部的文件夹，过滤 掉自己
        List<WmFolders> all = wmFoldersMapper.selectList(new QueryWrapper<WmFolders>().eq("status", 0).ne("user_id", uid));

        //查询好友绑定表，好友的文件夹
        List<WmUserFriend> friendList = wmUserFriendMapper.selectList(new QueryWrapper<WmUserFriend>().eq("friend_id", uid).eq("shared_status", "0"));
        if (CollectionUtils.isNotEmpty(friendList)) {
            friendList.forEach(wmUserFriend -> {
                List<WmFolders> wmFolders = wmFoldersMapper.selectList(new QueryWrapper<WmFolders>().eq("user_id", wmUserFriend.getUserId()).in("status", 1));
                list.addAll(wmFolders);
            });
        }
        //查询自己的文件夹

        list.addAll(meList);
        list.addAll(all);
        //按照时间排序
        return list.stream().sorted((o1, o2) -> o2.getCreatedTime().compareTo(o1.getCreatedTime())).collect(Collectors.toList());
    }

    @Override
    public WmFolders getNameFolders(QueryWrapper<WmFolders> queryWrapper) {
        return wmFoldersMapper.selectOne(queryWrapper);
    }
}